//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
	dataType: "json",
	cache: false
});

function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    return selectedIDs[0];
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}


function showFile(isImg, callback) {
	
	$("#fileJqGrid").jqGrid({
        url: '../filemanager/list?isImg=' + isImg,
        datatype: "json",
        colModel: [
            //{ label: 'id', name: 'id', index: 'id', width: 80, key: true },
			{ label: '名称', name: 'name', index: 'name', width: 80},
			{ label: '文件名', name: 'fileName', index: 'file_name', width: 80 },
			{ label: 'shortUrl', name: 'shortUrl', index: 'short_url', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: 185,
        width:590,
        rowNum: 5,
		rowList : [5,10,20],
        rownumbers: false, 
        multiselect: true,
        pager: "#fileJqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#fileJqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
	
	layer.open({
		type:1,
		title: "请选择",
		area: ['600px', '420px'],
		shadeClose: false,
		shade: 0.000001,
		anim : 4,
		resize : true,
		content: $("#showFile"),
		btn: ['确认', '取消'],
		btn1: function (index) {
			var grid = $("#fileJqGrid");
		    var rowKey = grid.getGridParam("selrow");
		    if(!rowKey){
		    	alert("请选择一条记录");
		    	return ;
		    }
		    
		    var selectedIds = grid.getGridParam("selarrrow");
		    if(selectedIds.length > 1){
		    	alert("只能选择一条记录");
		    	return ;
		    }
		    var data = grid.getRowData(selectedIds[0]);
		    callback(data.name, data.shortUrl);
			layer.close(index);
        },
        btn2: function (index) {
			layer.close(index);
        }
	});
}
function queryfile() {
	var page = $("#fileJqGrid").jqGrid('getGridParam','page');
	$("#fileJqGrid").jqGrid('setGridParam',{ 
        postData:{'key': vm.q.filekey},
        page:page
    }).trigger("reloadGrid");
}
































